package kattmoy.lucene.text;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ContextInitialized {
	
	public static void main(String[] args) throws DocumentException, IOException, ParseException {
//		String url = "http://news.qq.com/newsgn/rss_newsgn.xml";
//		connectAndIndexer(url);
		String keywords = "QQ";
		searcher(keywords);
	}   
	
	public static void connectAndIndexer(String url) throws DocumentException, IOException{
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		URL U = new URL(url);
		URLConnection connection = U.openConnection();
		connection.connect();
		SAXReader reader = new SAXReader();
	    Document doc = reader.read(connection.getInputStream());
		
	    Element root = doc.getRootElement();
	    Element foo;
	    Element subfoo;
	    
	    int ios = 1;
	    
	    //分词器
	    Analyzer a = new StandardAnalyzer();
	    //索引文档的存储位置 FSDirectory将索引写入硬盘，RAMDirectory将索引写入内存
	    Directory dir = FSDirectory.open(Paths.get("./index"));
	    //操作索引库的配置信息，传入分词器
	    IndexWriterConfig iwc = new IndexWriterConfig(a);
	    //建立索引的核心类，用来操作索引（增、删、改），传入索引文档存储位置以及操作索引库的配置信息
	    IndexWriter iw = new IndexWriter(dir, iwc);
	    
	    for(Iterator<Element> i = root.elementIterator("channel"); i.hasNext();){
	    	foo = i.next();
	    	for(Iterator<Element> j = foo.elementIterator("item");j.hasNext();){
	    		System.out.println("---------------------------------------------------");
	    		subfoo = j.next();
	    		String link = subfoo.elementTextTrim("link");
	    		String title = subfoo.elementTextTrim("title").trim();
	    		String content = subfoo.elementTextTrim("description");
	    		String author = subfoo.elementTextTrim("author");
	    		
	    		System.out.println("author=="+author);
	    		System.out.println("title=="+title);
	    		System.out.println("link=="+link);
	    		System.out.println("description=="+content);
	    		
	    		org.apache.lucene.document.Document document = new org.apache.lucene.document.Document();
	    		
	    		document.add(new StringField("Id",String.valueOf(ios++),Store.YES));
	    		document.add(new TextField("title",title,Store.YES));
	    		document.add(new TextField("content",content,Store.YES));
	    		document.add(new StringField("author",author,Store.YES));
	    		document.add(new StringField("link",link,Store.YES));
	    		document.add(new StringField("date",sdf.format(new Date()),Store.YES));
	    		iw.addDocument(document); 
	    	}
	    	
	    }
		 iw.close();
	     dir.close();

	}
	
	public static void searcher(String keywords) throws IOException, ParseException{

    	//分词器
        Analyzer a = new StandardAnalyzer();
		//从硬盘上获取索引文件
        Directory dir = FSDirectory.open(Paths.get("./index"));
        //读取索引的工具类，常用子类有DirectoryReader
        IndexReader reader = DirectoryReader.open(dir);
        //查询索引的核心类
        IndexSearcher is = new IndexSearcher(reader);
        
        //查询分析器，表示从哪里查用哪个分析器
        QueryParser parser = new QueryParser("content", a);
        //代表一次查询
        Query query = parser.parse(keywords);
        
        //封装了匹配情况，比如匹配多少个  totalHits 表示匹配的个数
        TopDocs topDocs = is.search(query, 1000);
        System.out.println("总共匹配多少个：" + topDocs.totalHits);
        
        //匹配的数据，里面封装了索引文档的得分和索引ID
        ScoreDoc[] hits = topDocs.scoreDocs;
        
        // 应该与topDocs.totalHits相同
        System.out.println("多少条数据：" + hits.length);
        for (ScoreDoc scoreDoc : hits) {
        	System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        	
            System.out.println("匹配得分：" + scoreDoc.score);
            System.out.println("文档索引ID：" + scoreDoc.doc);
            
            org.apache.lucene.document.Document document = is.doc(scoreDoc.doc);
            System.out.println(document.get("content"));
        }
        
        reader.close();
        dir.close();
	}
}
